package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.db.*;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.ErrorResult;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import com.itheima.vo.UserInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsk
 * @create 2022-03-14-19:45
 */
@Component
public class SettingManager {

    @DubboReference
    private QuestionService questionService;

    @DubboReference
    private NotificationService notificationService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @DubboReference
    private UserService userService;

    @DubboReference
    private BlackListService blackListService;

    @DubboReference
    private UserInfoService userInfoService;

    //查询通用设置
    public ResponseEntity settings() {

        //获取线程内ThreadLocal
        User user = UserHolder.get();
        //调用questionService查询陌生人问题
        Question question = questionService.findByUserId(user.getId());

        //调用notificationService查询推送设置
        Notification notification = notificationService.findByUserId(user.getId());

        //封装到SettingVo对象中
        SettingVo vo = new SettingVo();
        vo.setId(user.getId());//封装id
        if (question != null) {//如果问题不为null则封装
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }
        if (notification != null) {//如果推送不为null则封装
            vo.setGonggaoNotification(notification.getGonggaoNotification());
            vo.setPinglunNotification(notification.getPinglunNotification());
            vo.setLikeNotification(notification.getLikeNotification());
        }
        vo.setPhone(user.getMobile());//封装电话

        //返回vo结果
        return ResponseEntity.ok(vo);

    }

    //校验验证码
    public ResponseEntity checkSendSms(String verificationCode, String mobile) {

        //从Redis中取出验证码
        String redisCode = stringRedisTemplate.opsForValue().get(ConstantUtil.SMS_CODE + mobile);

        Boolean verification;

        //进行判断
        if (!StringUtils.equals(redisCode, verificationCode)) {
            verification = false;
            //抛出错误
            return ResponseEntity.status(500).body(ErrorResult.loginError());
        } else {
            verification = true;
        }
        Map resultMap = new HashMap();
        resultMap.put("verification", verification);
        return ResponseEntity.ok(resultMap);
    }

    //修改手机号
    public void updatePhone(String phone) {

        //通过ThreadLocal获取当前user信息
        Long id = UserHolder.get().getId();

        //根据id查询当前用户
        User user = userService.findById(id);
        //设置user手机号
        user.setMobile(phone);
        //调用service修改
        userService.update(user);
    }

    //设置陌生人问题
    public void setQuestion(String content) {

        //查询当前UserId
        Long userId = UserHolder.get().getId();

        //根据id查询当前用户陌生人问题数据
        Question question = questionService.findByUserId(userId);

        //判断
        if (question == null){//为空代表是新用户
            //新增问题
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            //调用添加
            questionService.save(question);
        }else {
            //代表已有问题，则修改原有问题
            question.setStrangerQuestion(content);
            questionService.update(question);
        }

    }

    //通知设置
    public void setNotification(Notification notificationParam) {
        //先获取当前登录userId
        Long userId = UserHolder.get().getId();

        //查询当前用户系统设置
        Notification notification = notificationService.findByUserId(userId);

        //判断当前用户有无系统设置
        if (notification == null){//没有设置
            //补全参数
            notificationParam.setUserId(userId);
            //调用service添加
            notificationService.save(notificationParam);
        }else {//已经设置过，修改系统设置
            //修改根据当前用户id修改
            notificationParam.setId(notification.getId());
            notificationService.update(notificationParam);
        }

    }

    //分页显示黑名单
    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {
        //获取当前用户userId
        Long userId = UserHolder.get().getId();
        //分页查询当前用户黑名单信息
        PageBeanVo pageBeanVo = blackListService.findByPageBlack(userId,pageNum,pageSize);
        //显示当前列表(转换为lise集合对象)
        List<BlackList> blackList = (List<BlackList>) pageBeanVo.getItems();
        //新建一个用户信息集合对象
        List<UserInfo> userInfoList = new ArrayList<>();
        //非空判断
        if (CollectionUtil.isNotEmpty(blackList)){
            for (BlackList black : blackList) {
                //取出黑名单id
                Long blackUserId = black.getBlackUserId();
                //根据黑名单id查询userInfo信息
                UserInfo userInfo = userInfoService.findById(blackUserId);
                //存储到userInfoList集合中
                userInfoList.add(userInfo);
            }
        }
        //将信息封装到PageBeanVo对象中
        pageBeanVo.setItems(userInfoList);
        //返回前端结果
        return ResponseEntity.ok(pageBeanVo);
    }

    //删除黑名单中信息
    public void deleteById(Long blackId) {

        //获取线程内userId
        Long userId = UserHolder.get().getId();
        //调用service删除
        blackListService.deleteById(userId,blackId);

    }

}
